package reset

import (
	"fmt"
	"os"
	"path"

	"gopkg.openfuyao.cn/bkecommon/source"

	econd "gopkg.openfuyao.cn/bkeadm/pkg/executor/containerd"
	"gopkg.openfuyao.cn/bkeadm/pkg/global"
	"gopkg.openfuyao.cn/bkeadm/pkg/infrastructure"
	"gopkg.openfuyao.cn/bkeadm/pkg/initialize/syscompat"
	"gopkg.openfuyao.cn/bkeadm/pkg/root"
	"gopkg.openfuyao.cn/bkeadm/pkg/server"
	"gopkg.openfuyao.cn/bkeadm/utils"
	"gopkg.openfuyao.cn/bkeadm/utils/log"
)

type Options struct {
	root.Options
	Args  []string `json:"args"`
	All   bool     `json:"all"`
	Mount bool     `json:"mount"`
}

func (op *Options) Reset() {
	RemoveLocalKubernetes()
	if op.All {
		RemoveContainerService()
		RemoveNtpService()
		removeAllInOne()
		err := source.ResetSource()
		if err != nil {
			log.BKEFormat(log.WARN, fmt.Sprintf("Reset source error: %s", err.Error()))
		}
		err = syscompat.RepoUpdate()
		if err != nil {
			log.BKEFormat(log.WARN, fmt.Sprintf("Update source error: %s", err.Error()))
		}
	}
	if op.Mount {
		RemoveDataDir()
		if !op.All {
			RemoveContainerService()
			RemoveNtpService()
			err := source.ResetSource()
			if err != nil {
				log.BKEFormat(log.WARN, fmt.Sprintf("Reset source error: %s", err.Error()))
			}
		}
	}
	log.BKEFormat(log.INFO, "BKE reset completed")
}

func RemoveLocalKubernetes() {
	if infrastructure.IsDocker() {
		log.BKEFormat(log.INFO, "Remove local Kubernetes")
		_ = global.Docker.ContainerRemove(utils.LocalKubernetesName)
		_ = os.RemoveAll("/etc/rancher")
		_ = os.RemoveAll("/var/lib/rancher")
		return
	}
	if infrastructure.IsContainerd() {
		log.BKEFormat(log.INFO, "Remove local Kubernetes")
		_ = econd.ContainerRemove(utils.LocalKubernetesName)
		_ = os.RemoveAll("/etc/rancher")
		_ = os.RemoveAll("/var/lib/rancher")
		return
	}
}

func RemoveContainerService() {
	_ = server.RemoveImageRegistry(utils.LocalImageRegistryName)
	_ = server.RemoveChartRegistry(utils.LocalChartRegistryName)
	_ = server.RemoveYumRegistry(utils.LocalYumRegistryName)
	_ = server.RemoveNFSServer(utils.LocalNFSRegistryName)
}

func RemoveNtpService() {
	_ = server.RemoveNTPServer()
}

func RemoveDataDir() {
	err := os.RemoveAll(path.Join(global.Workspace, "mount"))
	if err != nil {
		log.BKEFormat(log.ERROR, fmt.Sprintf("Remove mount dir error: %s", err.Error()))
	}
}

func removeAllInOne() {
	cleanKubeletBin()
	cleanKubernetesContainer()
	cleanOtherContainer()
	cleanContainerRuntime()
	cleanNetwork()
	cleanNeedDeleteFile()
}
